<?php
function form_input($form,$namn,$rubrik = '',$val='',$typ="text",$error='') {
	if(!$rubrik){ $rubrik = $namn;}
	$html = '
		  <tr>
			<th scope="row">%s</th>
			<td>
			
				<div class="input_left"></div>
				<input type="%s" class="t_input" id="%s_%s" name="%s_%s" value="%s" />
				<div class="input_right" id="%s_%s_error">%s</div>
			</td>
		  </tr>';
	return sprintf($html,$rubrik,$typ,$form,$namn,$form,$namn,$val,$form,$namn,$error);
}

function form_checkbox ($form,$namn,$rubrik = '',$val='') {
	if(!$rubrik){ $rubrik = $namn;}
	if($val == 'on'){$checked = 'checked="checked"';}else{$checked = '';}
	$html = '
	<tr>
	<th colspan="2" scope="row"><label for="%s_%s">%s</label>
	<input type="checkbox" id="%s_%s" name="%s_%s" %s /></th>
	</tr>';
	return sprintf($html,$form,$namn,$rubrik,$form,$namn,$form,$namn,$checked);
}

function form_html_outer($form,$action,$head,$btn)
{
	$html = sprintf('
	<h2>%s</h2>
	<form id="%s" name="%s" method="post" action="%s">
	<table border="0" cellpadding="0" cellspacing="0">
	
	%s
	
	<tr>
	<th scope="row"><input type="submit" name="Submit" id="button" value="Submit" /></th>
	<td><input name="%s_submit" id="%s_submit" type="hidden" value="1" />
	<a href="#" class="btn_a" onclick="submitform()"><span class="btn_left"></span><span class="btn">%s</span><span class="btn_right"></span></a>
	</td>
	</tr>
	
	</table>
	</form>
	',$head,$form,$form,$action,'%s',$form,$form,$btn);	;
	return $html;
}




function trigger__error($id,$error)
{
	#print $id.' - '.$error."<br />\n";
}

function check_data($data,$spec=0)
{
	if($spec)
	{
		$data = str_replace("-", "", $data);
		$data = str_replace(" ", "", $data);
	}
	 $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
	return $data;
}
function validateEmail($email)
{
	return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i"
	,$email));
}
function isEmpty($s,$len = 2)
{
	return ( ($s == '') || (strlen($s) < $len) );
}
function check_is_str($id,$error)
{
	if(isset($_POST[$id]))
	{
		$s = trim($_POST[$id]);
		if(isEmpty($s))
		{
			trigger__error($id,$error);
			return false;
		}
		return true;
		
	}
	return false;
	
}

function check_is_int($id,$error)
{
	if(isset($_POST[$id]))
	{
		$s = $_POST[$id];
		if(preg_match ("/[^\s0-9]/", $s) || (strlen($s) <= 3))
		{
			trigger__error($id,$error);
			return false;
		}
		return true;
		
	}
	return false;
}

function check_pnr($id,$error) { 
	if(isset($_POST[$id]))
	{
		$s = trim($_POST[$id]);
		$pnr = str_replace("-", "", $s);
		if (strlen($pnr) != 10) 
		{
			return false;
		}
		$n = 2;
		for ($i=0; $i<9; $i++) { 
			$tmp = $pnr[$i] * $n; 
			($tmp > 9) ? $sum += 1 + ($tmp % 10) : $sum += $tmp; 
			($n == 2) ? $n = 1 : $n = 2; 
		}
	 
		return !( ($sum + $pnr[9]) % 10);
	}
	return false;
}

function check_is_email($id,$error)
{
	if(isset($_POST[$id]))
	{
		$s = trim($_POST[$id]);
		if(!validateEmail($s))
		{
			trigger__error($id,$error);
			return false;
		}
		return true;
		
	}
	return false;
}

function check_pass($id,$error)
{
	if(isset($_POST[$id]))
	{
		$s = trim($_POST[$id]);
		if(isEmpty($s,5))
		{
			trigger__error($id,$error);
			return false;
		}
		return true;
		
	}
	return false;
}

function check_pass2($id,$id2,$error)
{
	if(isset($_POST[$id]) && isset($_POST[$id2]))
	{
		$s  = trim($_POST[$id]);
		$s2 = trim($_POST[$id2]);
		if( (isEmpty($s,5)) || ($s != $s2) )
		{
			trigger__error($id,$error);
			return false;
		}
		return true;
		
	}
	return false;
}
?>